package dao;

import java.util.Collection;

import models.Subject;

import org.hibernate.Session;

import utils.HbnUtil;

public class SubjectDaoHbnImpl implements SubjectDao {

	@Override
	public void addSubject(Subject sub) {
		Session session = HbnUtil.getSession();
		session.saveOrUpdate(sub);
	}

	@Override
	public void delSubject(Long subId) {
		Session session = HbnUtil.getSession();
		Subject sub=selSubjectById(subId);
		session.delete(sub);

	}

	@Override
	public Collection<Subject> selAllSubject() {
		Session session = HbnUtil.getSession();
		String hql="from Subject s";
		return session.createQuery(hql).list();
	}

	@Override
	public Subject selSubjectById(Long subId) {
		Session session = HbnUtil.getSession();
		String hql="from Subject s where s.id=:subId";
		return (Subject) session.createQuery(hql).setLong("subId", subId)
				.uniqueResult();
	}

	@Override
	public void updateSubject(Subject sub) {
		addSubject(sub);
	}

	@Override
	public Subject selSubjectMoreById(Long subId) {
		Session session = HbnUtil.getSession();
		String hql="from Subject s left join fetch s.paperOfList where s.id=:subId";
		return (Subject) session.createQuery(hql).setLong("subId", subId)
				.uniqueResult();
	}

}
